import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
// 引入多个模块的规则
import approvalsRouter from './modules/approvals'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionRouter from './modules/permission'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import settingRouter from './modules/setting'
import socialRouter from './modules/social'
/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
//  静态路由规则(没有权限的页面)
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  // 这一个规则就是首页的规则
  {
    // hidden: true, // 只要加了这个属性, 不会在左侧导航显示了
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'dashboard',
      component: () => import('@/views/dashboard'),
      // meta是路由元信息, title是控制文本 icon是控制图标的
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },
  // 上传组件的规则
  {
    hidden: true, // 只要加了这个属性, 不会在左侧导航显示了
    path: '/import',
    component: Layout,
    children: [{
      path: '/import',
      name: 'import',
      component: () => import('@/views/Import')
      // meta是路由元信息, title是控制文本 icon是控制图标的
      // meta: { title: '上传', icon: 'dashboard' }
    }]
  }
  // 404 页面放在最后匹配
]
//  动态路由规则 (有权限的页面)
export const asyncRoutes = [
  departmentsRouter,
  settingRouter,
  employeesRouter,
  permissionRouter,
  approvalsRouter,
  attendancesRouter,
  salarysRouter,
  socialRouter
]
const createRouter = () => new Router({
  mode: 'history', // 服务端支持 目前处于webpack环境
  scrollBehavior: () => ({ y: 0 }),
  // 目前处于开发阶段, 应该以上帝视角开发所有页面,至于权限页面控制,最后来写
  routes: [
    ...constantRoutes
    // ...asyncRoutes,
    // { path: '*', redirect: '/404', hidden: true }
  ]
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
// 这三行目前用不到, 做路由重置的, 最后处理权限的时候再来看它
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
